package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.happypets.daoImpl.PetDaoImpl;
import com.happypets.model.UserBean;
import com.happypets.servicesImpl.UserServiceImpl;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LoginServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		PetDaoImpl dao = new PetDaoImpl();
		UserServiceImpl userSer = new UserServiceImpl();
		
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		Gson gson=new Gson();
		Map<String,String> map = new HashMap<String,String>();
		String flag = request.getParameter("flag");
		
		if(flag.equals("1")){
			//login
			String name = request.getParameter("name");
			String passwd = request.getParameter("password");
			
			int tag = userSer.login(name, passwd);
			
			
			
			
			switch(tag){
			case 1:{
							map.put("flag", "1");
							map.put("msg", "success");
							UserBean user = userSer.getUserBeanByName(name);
							session.setAttribute("user", user);
			}
				break;
			case 2:map.put("flag", "2");
				map.put("msg", "没有这个账号！");
				break;
			default:map.put("flag", "3");
				map.put("msg", "密码错误！");
			}
			
			
		}else{
			//regist
			String name = request.getParameter("name");
			String passwd = request.getParameter("passwd");
			String sex = request.getParameter("sex");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String provience = request.getParameter("provience");
			String city = request.getParameter("city");
			String country = request.getParameter("country");
			String address = request.getParameter("address");
			
			UserBean user = new UserBean();
			
			user.setPassword(passwd);
			user.setUserAddress(address);
			user.setUserAddPro(provience);
			user.setUserAddCity(city);
			user.setUserAddCou(country);
			user.setUserEmail(email);
			user.setUserName(name);
			user.setUserPhone(phone);
			user.setUserSex(sex);
			
			int war = userSer.addUser(user);
			
			if(war == 1){
				//success
				map.put("flag", "1");
				map.put("msg","注册成功！");
				//登录
				user = userSer.getUserBeanByName(name);
				session.setAttribute("user", user);
			}else{
				map.put("flag", "2");
				map.put("msg","注册失败！");
			}
			
		}
		List<Map<String,String>> lm = new ArrayList<Map<String,String>>();
		lm.add(map);
		String json=gson.toJson(lm);
		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
